import Axios from 'common/js/axios'
import axios from 'axios'
import Config from '@/config'

export function uploadToken(cropWH = '') {
	const url = Config.uploadUrl + '/oss/sign'
	// return Axios.get(url, { optionCropWH: cropWH })
	return axios({
		method: 'GET',
		url: Config.uploadUrl + '/oss/sign',
		// data: { optionCropWH: cropWH }
	})
}

export function uploadImage(file, cropWH = '') {
	return new Promise((resolve, reject) => {
		if (file.size > 1024 * 1024) {
			reject("上传的图片大小不能大于1M")
		}
		uploadToken(cropWH).then(res => {
			let key = new Date().toLocaleDateString() + '/' + new Date().getTime() + '.png'
			console.log(1243, res,"res")
			var formData = new FormData()
			formData.append("key", key)
			formData.append("policy", res.data.policy)
			formData.append("OSSAccessKeyId", res.data.accessid)
			formData.append("success_action_status", "200")
			formData.append("signature", res.data.signature)
			formData.append("file", file)
			formData.append("host", res.data.host)
			// formData.append('callback', callbackbody)
			axios({
				method: 'POST',
				url: res.data.host,
				data: formData,
				headers: {
					'Content-Type': 'multipart/form-data'
				}
			}).then(_data => {
				var imgUrl = res.data.host + '/' + formData.get("key");
				resolve(imgUrl)
			}).catch(err => {
				reject(err)
			})
		}).catch(err => {
			reject(err)
		})
	})
}